<?php

class HmMailController extends Fw_Controller_Action
{
	
	public function init()
	{
		$this->setDisplay('backoffice');
		$this->initSession();
	}

	public function sendAction()
	{
		if($this->_request->isPost()) {
			$formData = $this->_request->getPost('edit');
			if($formData) {
				require_once('../../library/swift/lib/swift_required.php');
	
				$model = $this->_getModel('HmProject');
				$view_list = $this->_model;
				$rowset = $view_list->fetchRow(
									$view_list->select()
									->order('project_id DESC')
									->limit(1,0));
				
				$transport = Swift_SmtpTransport::newInstance($rowset->smtp_host, $rowset->smtp_port, 'ssl')
				  ->setUsername($rowset->smtp_login)
				  ->setPassword($rowset->smtp_pass);
	
				$mailer = Swift_Mailer::newInstance($transport);
				
				$message = Swift_Message::newInstance($this->_request->getPost('subject'))
				  ->setFrom(array($this->_request->getPost('from_email') => $this->_request->getPost('from_name')))
				  ->setTo(array($rowset->email => $rowset->name))
				  ->setBody($this->_request->getPost('message'));
				  
				//Send the message
				$result = $mailer->send($message);
				
				if($result) {
					$this->_helper->redirector('result');
				} else {
					return false;
				}
			}
		}
	}
	
	public function resultAction()
	{
		
	}
}